package feed.view;
import feed.dao.DetalleMovimientoDao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import feed.dao.MovimientoDao;
import feed.dao.TotalesDao;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Movimiento extends javax.swing.JInternalFrame {

    public Movimiento() {
        try{           
            initComponents();//siempre solo un initcomp...
            lbldetalle.setVisible(false);
            this.txttotaldebe.setVisible(false);
            this.txttotalhaber.setVisible(false);
            this.txtidmovimiento.setVisible(false);
            this.jButton1.setVisible(false);
            //cbidmov.setVisible(false);
            DefaultComboBoxModel modeloCombo = new DefaultComboBoxModel();
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            Connection conexion = DriverManager.getConnection("jdbc:derby://localhost:1527/dbconta", "root", null);
            Statement st = conexion.createStatement();
            ResultSet rs = st.executeQuery("select id, nombre from app.tipomovimiento");
            while (rs.next()) {
                modeloCombo.addElement(rs.getObject("nombre"));
            }
            rs.close();
            cbtipo.setModel(modeloCombo);
        } catch (SQLException ex) {
            Logger.getLogger(Movimiento.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Movimiento.class.getName()).log(Level.SEVERE, null, ex);
        }
         try {
            DefaultComboBoxModel modelCombo = new DefaultComboBoxModel();
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            Connection conexion = DriverManager.getConnection("jdbc:derby://localhost:1527/dbconta", "root", null);
            Statement st = conexion.createStatement();
            ResultSet rs = st.executeQuery("select id, nombre from app.cuenta");
            while (rs.next()) {
                modelCombo.addElement(rs.getObject("nombre"));
            }
            rs.close();
            cbcuenta.setModel(modelCombo);
        } catch (SQLException ex) {
            Logger.getLogger(Movimiento.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Movimiento.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        cbtipo = new javax.swing.JComboBox();
        lbltipo = new javax.swing.JLabel();
        lblfecha = new javax.swing.JLabel();
        lblcomentario = new javax.swing.JLabel();
        txtcomentario = new javax.swing.JTextField();
        btnok = new javax.swing.JButton();
        lbldetalle = new javax.swing.JLabel();
        txtfecha = new com.toedter.calendar.JDateChooser();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtdebe = new javax.swing.JTextField();
        cbcuenta = new javax.swing.JComboBox();
        btninsertar = new javax.swing.JToggleButton();
        btnterminar = new javax.swing.JToggleButton();
        txtidmovimiento = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        txthaber = new javax.swing.JTextField();
        txttotalhaber = new javax.swing.JTextField();
        txttotaldebe = new javax.swing.JTextField();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Encabezado"));

        lbltipo.setText("Tipo");

        lblfecha.setText("Fecha");

        lblcomentario.setText("Comentario");

        txtcomentario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtcomentarioActionPerformed(evt);
            }
        });

        btnok.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Ok-icon.png"))); // NOI18N
        btnok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnokActionPerformed(evt);
            }
        });

        lbldetalle.setText("INGRESA DETALLE");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblcomentario)
                    .addComponent(lbltipo))
                .addGap(36, 36, 36)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(cbtipo, 0, 171, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(lblfecha)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtfecha, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txtcomentario))
                .addGap(42, 42, 42)
                .addComponent(btnok, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(139, 139, 139))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(299, 299, 299)
                .addComponent(lbldetalle, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(120, 120, 120))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(cbtipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(lbltipo)
                                .addComponent(lblfecha))
                            .addComponent(txtfecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblcomentario)
                            .addComponent(txtcomentario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnok, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)))
                .addComponent(lbldetalle))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Detalle"));

        jLabel1.setText("CUENTA");

        jLabel2.setText("DEBE");

        jLabel3.setText("HABER");

        txtdebe.setText("0");
        txtdebe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtdebeActionPerformed(evt);
            }
        });
        txtdebe.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
                txtdebeCaretPositionChanged(evt);
            }
        });
        txtdebe.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtdebeKeyTyped(evt);
            }
        });

        btninsertar.setText("INSERTAR");
        btninsertar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btninsertarActionPerformed(evt);
            }
        });

        btnterminar.setText("TERMINAR");
        btnterminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnterminarActionPerformed(evt);
            }
        });

        jButton1.setText("CargarID");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        txthaber.setText("0");
        txthaber.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txthaberActionPerformed(evt);
            }
        });
        txthaber.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txthaberKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtidmovimiento)
                    .addComponent(cbcuenta, 0, 140, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(txtdebe, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(txthaber, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btninsertar, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnterminar, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButton1))
                .addGap(0, 25, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(64, 64, 64)
                .addComponent(jLabel1)
                .addGap(107, 107, 107)
                .addComponent(jLabel2)
                .addGap(103, 103, 103)
                .addComponent(jLabel3)
                .addContainerGap(366, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtdebe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbcuenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btninsertar)
                    .addComponent(btnterminar)
                    .addComponent(txthaber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtidmovimiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addComponent(txttotaldebe, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addComponent(txttotalhaber, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txttotalhaber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txttotaldebe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(51, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtcomentarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtcomentarioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtcomentarioActionPerformed
    
    private void btnokActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnokActionPerformed
        int tipo = cbtipo.getSelectedIndex();
        int idtipo = tipo + 1;
        System.out.println("idtipo:"+idtipo);
        //Inicio JDATECHOSER CONVIRTIENDO FORMATO DE FECHA A ESTRING
        SimpleDateFormat formato = new SimpleDateFormat("yyyy/MM/dd");
        Date fechaA = txtfecha.getDate();
        String fecha=formato.format(fechaA);
        //FIN LISTO PARA INGRESAR LLAMANDO A VARIABLE 'FECHA'
        String comentario = txtcomentario.getText();
        MovimientoDao md = new MovimientoDao();
        md.IngresarM(idtipo,fecha,comentario);
        System.out.println(""+fecha);
        txtfecha.setVisible(false);
        txtcomentario.setVisible(false);
        cbtipo.setVisible(false);
        lblcomentario.setVisible(false);
        lblfecha.setVisible(false);
        lbltipo.setVisible(false);
        lbldetalle.setVisible(true);
        btnok.setVisible(false);
        System.out.println("aqui termina el ok");
    }//GEN-LAST:event_btnokActionPerformed

    private void btninsertarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btninsertarActionPerformed
        MovimientoDao mov=new MovimientoDao();
            txtidmovimiento.setText(Integer.toString(mov.findultimaid()));
            int idfinal=mov.findultimaid();
            System.out.println("el ultimo id es="+idfinal);
        int idcuenta=this.cbcuenta.getSelectedIndex();
        String nomidcuenta=cbcuenta.getSelectedItem().toString();
            idcuenta=idcuenta+1;
            System.out.println("cuenta="+nomidcuenta+", id="+idcuenta);
        int debe=Integer.parseInt(txtdebe.getText());
        int haber=Integer.parseInt(txthaber.getText());
        DetalleMovimientoDao dmd=new DetalleMovimientoDao();
        System.out.println("el envio es="+idfinal+","+idcuenta+","+debe+","+haber);
        dmd.IngresarDM(idfinal, idcuenta, debe, haber);
        limpiar();
        
    }//GEN-LAST:event_btninsertarActionPerformed

    private void txtdebeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtdebeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtdebeActionPerformed

    private void btnterminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnterminarActionPerformed
        TotalesDao tdo=new TotalesDao();
        MovimientoDao mov=new MovimientoDao();
        int idfinal=3;//mov.findultimaid();
        int tdebe, thaber;
        tdebe=Integer.parseInt(txttotaldebe.getText());
        thaber=Integer.parseInt(txttotalhaber.getText());
        
    }//GEN-LAST:event_btnterminarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
            
           
    }//GEN-LAST:event_jButton1ActionPerformed

    private void txtdebeKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtdebeKeyTyped
       
    }//GEN-LAST:event_txtdebeKeyTyped

    private void txtdebeCaretPositionChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_txtdebeCaretPositionChanged

    }//GEN-LAST:event_txtdebeCaretPositionChanged

    private void txthaberKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txthaberKeyTyped
        
    }//GEN-LAST:event_txthaberKeyTyped

    private void txthaberActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txthaberActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txthaberActionPerformed
        public void limpiar(){
        txtdebe.setText("0");
        txthaber.setText("0");
        }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton btninsertar;
    private javax.swing.JButton btnok;
    private javax.swing.JToggleButton btnterminar;
    private javax.swing.JComboBox cbcuenta;
    private javax.swing.JComboBox cbtipo;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblcomentario;
    private javax.swing.JLabel lbldetalle;
    private javax.swing.JLabel lblfecha;
    private javax.swing.JLabel lbltipo;
    private javax.swing.JTextField txtcomentario;
    private javax.swing.JTextField txtdebe;
    private com.toedter.calendar.JDateChooser txtfecha;
    private javax.swing.JTextField txthaber;
    private javax.swing.JTextField txtidmovimiento;
    private javax.swing.JTextField txttotaldebe;
    private javax.swing.JTextField txttotalhaber;
    // End of variables declaration//GEN-END:variables
}
